package com.aqie.easy.array;

/**
 * 896 单调数列 todo
 */
public class IsMonotonic {
    public boolean isMonotonic(int[] A) {
        int store = 0;
        for (int i = 0; i < A.length - 1; ++i) {
            int c = Integer.compare(A[i], A[i+1]);
            if (c != 0) {
                if (c != store && store != 0)
                    return false;
                store = c;
            }
        }
        return true;
    }

    /**
     * 两个变量 记录数组长度
     * @param A
     * @return
     */
    public boolean isMonotonic2(int[] A) {
        if(A.length <= 2) return true;
        int count1 = 1, count2 = 1;
        for(int i = 1; i < A.length; i++){
            if(A[i] > A[i-1]){
                count1++;
            }else if(A[i] < A[i-1]) {
                count2++;
            }else{
                count1++;
                count2++;
            }
        }
        return count1 == A.length || count2 == A.length;
    }

}
